// subPages/communityService/searchPage/searchPage.js
const app = getApp();
var utils = require('../../../utils/util');
var loadMoreView,p = 1,num = 4,type=''

Page({

  /**
   * 页面的初始数据
   */
  data: {
      themeColor:app.globalData.themeColor,
      btnText:'搜索',
      placeholderText:'搜索您感兴趣的商品~',
      goodsList:[
          // {
          //   product_name:'akdjdjfkk'
          // }
      ]
  },
    goodsListEvent(e){
        let id = e.currentTarget.dataset.id;
        let that = this;
        if(that.data.searchType == 0){
            wx.navigateTo({
                url:'/subPages/communityService/secondHandMallDetails/secondHandMallDetails?id='+id
            })
        }else{
            let class_id = e.currentTarget.dataset.class_id;
            let goods_id = e.currentTarget.dataset.goods_id;
            wx.navigateTo({
                url:'/subPages/communityService/shopContent/shopContent?id='+id+'&class_id='+class_id +'&goods_id='+goods_id
            })
        }

    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      //type 1 社区商城   0 二手市场
      let type = options.type;
      this.setData({
          searchType:type
      })
      loadMoreView = this.selectComponent("#loadMoreView");
      utils.setNavBarTitle('搜索')
  },
  getInputText(e){
    let like = e.detail.value;
    console.log(like);
    this.setData({
        inputText:like
    })
  },
    searchContent(){
        let _this = this;
        // console.log(_this.data.inputText);
        let apiUrl = '';
        if(_this.data.searchType == 0){
            apiUrl = '/get_old_product_like'
        }else if(_this.data.searchType == 1){
            apiUrl = '/get_community_service_shop_product_like'
        }
        if(_this.data.inputText != undefined){
        utils.getAjax(`${app.globalData.apiUrl}${apiUrl}`,res=>{
            console.log(res);
            if(res.data.list.length != 0){
              var list = [];
                if(p == 1){
                    list = res.data.list;
                }else{
                    list = _this.data.goodsList.concat(res.data.list)
                }
                _this.setData({
                    goodsList:list
                })
            }else{
                _this.setData({
                    goodsList:[]
                }) 
            }
            loadMoreView.loadMoreComplete(res.data,type);
        },{
            p:p,
            num:num,
            community_id:app.globalData.communityId,
            like:_this.data.inputText
        })
      }else{
        utils.showToast('搜索内容不能为空！','none',2000);
      }
    },
    //获取服务数据异常
    shopDataErr(err){
        if(p>1){
            loadMoreView.loadMoreFail()
        }else{
            util.showModal('提示','获取商品失败，请向上滑动屏幕')
            this.setData({
                isFail: true
            })
        }
    },
    loadMoreListener: function(e) {
        p += 1
        this.searchContent()
    },
    //
    clickLoadMore: function(e) {
        this.searchContent()
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.isFail){
          this.searchContent()
          return
      }
      loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
